package plugin.shiro;

import models.sys.AdminUser;
import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

/**
 * Created by zqx on 16/5/18.
 */
public class AdminPasswordHelper {

    private static final String algorithmName = "sha-256";
    private static final int hashIterations = 2;

    public static void encryptPassword(AdminUser user) {
        RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
        String salt = randomNumberGenerator.nextBytes().toHex();
        user.salt = salt;
        user.password = new SimpleHash(algorithmName, user.password, ByteSource.Util.bytes(salt), hashIterations).toHex();
    }

    public static String decipherPassword(String password, String salt) {
        return new SimpleHash(algorithmName, password, ByteSource.Util.bytes(salt), hashIterations).toHex();
    }

}
